<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row type="flex" justify="space-between" align="middle">
            <span>权限管理</span>
            <el-button type="primary" @click="addjurisdictionFn(1,'0')">添加权限</el-button>
          </el-row>
        </div>
        <!-- card body -->
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" type="text" @click="addjurisdictionFn(2,row.id)">添加</el-button>
              <el-button type="text" @click="compileFn(row)">编辑</el-button>
              <el-button type="text" @click="delFn(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog :visible.sync="showDialog" title="弹层标题" @close="close">
      <el-form ref="Form" label-width="100px" :model="formData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用" prop="type">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="addFn">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, addPermission, delPermission, updatePermission, getPermissionDetail } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      // 控制弹窗显隐
      showDialog: false,
      // 储存树桩权限列表
      list: [],
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限点名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限点标识不能为空', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getPermission()
  },
  methods: {
    // 获取权限信息列表
    async getPermission() {
      this.list = listToTreeData(await getPermissionList())
      console.table(this.list)
    },
    // 新增弹窗
    addjurisdictionFn(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // g关闭页面事件
    close() {
      // 关闭编辑弹窗需要还原原始数据
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
      this.$refs.Form.resetFields()
    },
    // 确认增加按钮
    addFn() {
      this.$refs.Form.validate(async(vali) => {
        if (vali) {
          // 修改
          if (this.formData.id) {
            await updatePermission(this.formData)

            this.$message.success('编辑成功')
          } else {
            // 新增
            await addPermission(this.formData)
            this.$message.success('新增成功')
          }

          this.showDialog = false
          this.getPermission()
          this.close()
        }
      })
    },
    // 删除权限
    delFn(row) {
      this.$confirm(`确认删除${row.name}吗`).then(async() => {
        if (row.children && row.children.length > 0) {
          return this.$message.error('该页面下有子权限。不可直接删除')
        } else {
          await delPermission(row.id)
          this.$message.success('删除成功')
          this.getPermission()
        }
      }).catch(() => {

      })
    },
    // 修改权限
    async compileFn(row) {
      this.showDialog = true
      const res = await getPermissionDetail(row.id)
      console.log('修改啊', res)
      this.formData = res
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
